package com.tao.action.menu;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.io.Serializable;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.opensymphony.xwork2.interceptor.annotations.Allowed;
import com.tao.action.BaseAction;
import com.tao.domain.ComboTree;
import com.tao.domain.Group;
import com.tao.domain.PolMenu;
import com.tao.domain.SubMenu;
import com.tao.domain.User;
import com.tao.service.group.GroupService;
import com.tao.service.menu.PolMenuService;
import com.tao.service.menu.SubMenuService;
import com.tao.service.user.UserService;
import com.tao.util.Page;

@SuppressWarnings("serial")
public class MenuAction extends BaseAction implements SessionAware{
	
	@Autowired
	private SubMenuService subMenuService; // 分局
	@Autowired
	private PolMenuService polMenuService; // 派出所
	@Allowed
	UserService userService;	// 微信号
	@Allowed
	GroupService groupService;	// 微信群
	SubMenu subMenu;
	PolMenu polMenu;
	@Allowed
	Map session;
	
	/**
	 * 增加分局
	 */
	public void addSubMenu(){
		subMenuService.add(subMenu);
	}
	
	/**
	 * 增加派出所
	 */
	public void addPolMenu(){
		polMenuService.add(polMenu);
	}
	
	/**
	 * 级联查询分局，派出所，微信号
	 */
	public void findPolSubWX(){
		String sql = "SELECT * FROM sub_menu AS s LEFT JOIN pol_menu AS p ON s.sub_id = p.sub_id LEFT JOIN sys_user AS u ON p.pol_id = u.pol_id";
		List list = polMenuService.queryAll(sql);
		System.out.println(list.get(0).toString());
	}
	@Override
	public String execute() throws Exception {
		
		return SUCCESS;
	}
	public void oneMenu() throws Exception {		
		List<SubMenu> subList = subMenuService.list("from SubMenu",null );
		getResult(new Gson().toJson(subList));
	}
	
	public void twoMenu() throws Exception {
		System.out.println(polMenu.getSub_id());
		Object[] objects = new Object[]{polMenu.getSub_id()};
		
		List<PolMenu> polList = polMenuService.list("from PolMenu where sub_id=?",objects);
		System.out.println(new Gson().toJson(polList));
		getResult(new Gson().toJson(polList));
	}
	
    @SuppressWarnings("unchecked")
	public void tree() {
    	
    	if(null==session.get("json")){
    	System.out.println("-------------service");
		List<ComboTree> list = new ArrayList<ComboTree>(); // 构建JSON数据
		
		List<SubMenu> subList = subMenuService.list("from SubMenu", null);
		 List<PolMenu> polList = polMenuService.list("from PolMenu", null);
		
		List<User> userlist = userService.findAll(null);
		List<Group> grouplist = groupService.findAll(null);


		
		// 一级菜单（分局）
		for (SubMenu subMenu : subList) {
			System.out.println("------"+subMenu.getSub_name());
			ComboTree one = new ComboTree();
			one.setId(String.valueOf(subMenu.getSub_id()));
			one.setText(subMenu.getSub_name());
			List<ComboTree> children = new ArrayList<ComboTree>();
			// 二级菜单(派出所)
			for (PolMenu polMenu : polList) {
				
				ComboTree two = new ComboTree();
				//一级分局Sub_id 和 二级派出所Sub_id 关联
				if (one.getId().equals(polMenu.getSub_id())) {
					two.setId(String.valueOf(polMenu.getPol_id()));
					two.setText(polMenu.getPol_name());
					children.add(two);
					// 三级菜单（微信号）
					List<ComboTree> children3 = new ArrayList<ComboTree>();
					for (User user : userlist) {
						// 二级派出所Pol_id 和 三级微信 Pol_id 关联
						ComboTree three = new ComboTree();
						if (polMenu.getPol_id().equals(user.getPol_id())) {
							
							three.setText(user.getNickName());
							children3.add(three);
							
							// 四级菜单（微信群）
							List<ComboTree> children4 = new ArrayList<ComboTree>();
							
							for (Group group : grouplist) {
								
								// 三级微信 Uin 和 四级微信群 Uin 关联
								if (group.getUin().equals(user.getUin())) {
									
									ComboTree groupthree = new ComboTree();
									groupthree.setText(group.getGroupNickName());
									groupthree.setUsername(user.getUserName());
									groupthree.setGroupname(group
											.getGroupName());
									children4.add(groupthree);
									
								}
							}
							
							three.setChildren(children4);
						}

					}
					two.setChildren(children3);
				}

			}

			one.setChildren(children);
			list.add(one);
		}

		String json = new Gson().toJson(list); // 转化为JSON
		
		json.replaceAll("children3", "children"); // 把children3转换成children
		json.replaceAll("children4", "children"); // 把children3转换成children
		
		session.put("json",json);
		
		System.out.println(json);
		getResult(json);
    	}else{
    		
    		getResult((String)session.get("json"));
    	}
		
	}
    public void getResult(String json){
    	System.out.println(json);
		 HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("UTF-8");
			try {
					response.getWriter().print(json);
					
			} catch (IOException e) {
			}
	}
	public SubMenuService getSubMenuService() {
		return subMenuService;
	}
	public void setSubMenuService(SubMenuService subMenuService) {
		this.subMenuService = subMenuService;
	}
	public PolMenuService getPolMenuService() {
		return polMenuService;
	}
	public void setPolMenuService(PolMenuService polMenuService) {
		this.polMenuService = polMenuService;
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public GroupService getGroupService() {
		return groupService;
	}
	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	
	public SubMenu getSubMenu() {
		return subMenu;
	}
	public void setSubMenu(SubMenu subMenu) {
		this.subMenu = subMenu;
	}
	public PolMenu getPolMenu() {
		return polMenu;
	}
	public void setPolMenu(PolMenu polMenu) {
		this.polMenu = polMenu;
	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}
    
}